package br.com.gbvbahia.mybooks.adapters;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import br.com.gbvbahia.mybooks.R;
import br.com.gbvbahia.mybooks.modelos.LivroVO;

public class LivroPesquisaResultAdapter extends BaseAdapter {
	private List<LivroVO> list;
	private LayoutInflater inflater;

	public LivroPesquisaResultAdapter(Activity context, List<LivroVO> list) {
		super();
		this.list = list;
		this.inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public LivroPesquisaResultAdapter(Fragment fragment, List<LivroVO> list) {
		this(fragment.getActivity(), list);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View view, ViewGroup parent) {
		ViewHolder holder = null;
		if (view == null) {
			holder = new ViewHolder();
			int layoutLinha = R.layout.livro_pesquisa_item;
			view = inflater.inflate(layoutLinha, null);
			view.setTag(holder);
			holder.tNome = (TextView) view.findViewById(R.id.tNome);
			holder.tNota = (TextView) view.findViewById(R.id.tNotaValor);
		} else {
			holder = (ViewHolder) view.getTag();
		}
		LivroVO vo = list.get(position);
		holder.tNome.setText(vo.getNomeLivro());
		if (vo.getNota() >= 0) {
			holder.tNota.setText(Integer.toString(vo.getNota()));
		} else {
			holder.tNota.setText("");
		}
		return view;
	}

	// Design Patter "ViewHolder" para Android
	static class ViewHolder {
		TextView tNome;
		TextView tNota;
	}
}
